/**
 * CharLoadWindow.java
 * 
 * @author	Joe Scharnitzke
 * Date:	06/10/2011
 * 
 * This file defines the GUI for loading a character
 * into a Character Sheet.
 * 
 * Change Log:
 * 	06/10/11 10:53 - (v0.1) Initial Implementation
 */

package gui;

import java.awt.event.*;
import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;
import javax.swing.*;

import java.util.*;
import java.awt.*;
import java.io.*;

public class CharLoadWindow extends JDialog implements ActionListener 
{
	private static final long serialVersionUID = -6166376375571850035L;
	
	private Vector<String> sv_chars;
	private Object[] oa_selected;
	private String[] sa_selected;
	
	private JScrollPane jsp_chars;
		private JList jli_chars;
	
	private JButton jb_load = new JButton("Load");
	private JButton jb_cancel = new JButton("Cancel");
	
	private GridBagConstraints gbc_main = new GridBagConstraints();
	
	/**
	 * Default constructor for CharLoadWidow objects. 
	 * 
	 * @param init_sv_chars a list of loadable characters
	 */
	
	public CharLoadWindow(Vector<String> init_sv_chars)
	{
		sv_chars = init_sv_chars;
		jli_chars = new JList(sv_chars);
		jli_chars.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		jli_chars.setLayoutOrientation(JList.VERTICAL);
		jsp_chars = new JScrollPane(jli_chars);
		
		jb_load.addActionListener(this);
		jb_cancel.addActionListener(this);
		
		setLayout(new GridBagLayout());
		
		//try to load the default logo
		try
		{
			BufferedImage bi_d20 = ImageIO.read(new File("assets/images/d20icon.png"));
			setIconImage(bi_d20);
		}
		
		catch(Exception e)
		{
			System.err.println(e);
		}
		
		gbc_main.fill = GridBagConstraints.BOTH;
		gbc_main.gridx = 0;
		gbc_main.gridy = 0;
		gbc_main.weightx = 1.0;
		gbc_main.weighty = 1.0;
		gbc_main.gridwidth = 2;
		gbc_main.gridheight = 1;
		gbc_main.insets = new Insets(2, 2, 2, 2);
		gbc_main.anchor = GridBagConstraints.NORTH;
		add(jsp_chars, gbc_main);
		
		gbc_main.fill = GridBagConstraints.NONE;
		gbc_main.gridx = 0;
		gbc_main.gridy = 1;
		gbc_main.weightx = 1.0;
		gbc_main.weighty = 0.0;
		gbc_main.gridwidth = 1;
		gbc_main.gridheight = 1;
		gbc_main.insets = new Insets(2, 2, 2, 2);
		gbc_main.anchor = GridBagConstraints.SOUTH;
		add(jb_load, gbc_main);
		
		gbc_main.fill = GridBagConstraints.NONE;
		gbc_main.gridx = 1;
		gbc_main.gridy = 1;
		gbc_main.weightx = 1.0;
		gbc_main.weighty = 0.0;
		gbc_main.gridwidth = 1;
		gbc_main.gridheight = 1;
		gbc_main.insets = new Insets(2, 2, 2, 2);
		gbc_main.anchor = GridBagConstraints.SOUTH;
		add(jb_cancel, gbc_main);
		
		setSize(250, 300);
		GUIHelper.Center(this);
		setTitle("Load");
		setModal(true);
		setVisible(true);
	}

	public void actionPerformed(ActionEvent event) 
	{
		if(event.getSource().equals(jb_cancel)) { dispose(); }
		
		if(event.getSource().equals(jb_load))
		{
			oa_selected = jli_chars.getSelectedValues();
			sa_selected = new String[oa_selected.length];
			
			for(int i = 0; i < sa_selected.length; i++)
			{
				sa_selected[i] = oa_selected[i].toString();
			}
			
			dispose();
		}
	}

	public String[] getSelected() {	return sa_selected;	}
}
